package com.fragrance.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.Model;

/**
 * 设备使用池表 t_device_using_pool
 * 
 * @author zhilin
 *
 */
public class UserDeviceModel extends Model<UserDeviceModel> {

	private final static Logger logger = LoggerFactory.getLogger(UserDeviceModel.class);

	private static final long serialVersionUID = 4976951028620631370L;

	public final static UserDeviceModel me = new UserDeviceModel();

	/**
	 * 查找
	 * 
	 * @param openId
	 * @param imei
	 * @return
	 */
	public UserDeviceModel findUserDevice(String imei, String openId, String deviceStatus) {
		return findFirst("select * from t_user_device where imei = ?  AND device_status = ?", imei,
				 deviceStatus);
	}

	/**
	 * 查找
	 * 
	 * @param imei
	 * @return
	 */
	public UserDeviceModel findUserDevice(String imei, String deviceStatus) {
		return findFirst("select * from t_user_device where imei = ? AND device_status = ?", imei, deviceStatus);
	}

	/**
	 * 如果表中存在device_status imei为0的状态的说明该机器已经有人在是使用
	 * 
	 * @param imei
	 * @return
	 */
	public UserDeviceModel isOpen(String imei) {
		List<UserDeviceModel> userDeviceModel = find("select * from t_user_device where imei=? and device_status = 0",
				imei);
		if (userDeviceModel != null && userDeviceModel.size() == 1) {
			return userDeviceModel.get(0);
		}
		return null;
	}
	
	/**
	 * 如果表中存在device_status imei为0的状态的说明该机器已经开机
	 * 
	 * @param imei openId
	 * @return
	 */
	public UserDeviceModel isBindOpen(Map<String,Object> data) {
		String imei = (String) data.get("imei");
		String openid = (String) data.get("openid");
		List<UserDeviceModel> userDeviceModel = find("select * from t_user_device where imei=? and  wx_open_id =? and device_status = 0 ",
				imei,openid);
		if (userDeviceModel != null && userDeviceModel.size() == 1) {
			return userDeviceModel.get(0);
		}
		return null;
	}

	/**
	 * 如果表中存在device_status imei为0的状态的说明该机器并wx_open_id为对应的用户
	 * 
	 * @param imei
	 * @return
	 */
	public UserDeviceModel isUse(String imei, String openid) {
		List<UserDeviceModel> userDeviceModel = find(
				"select * from t_user_device where imei=? and device_status = 0 ", imei);
		if (userDeviceModel != null && userDeviceModel.size() == 1) {
			return userDeviceModel.get(0);
		}
		return null;
	}

	/**
	 * 添加用户使用设备信息到记录表
	 * 
	 * @param data
	 * @return
	 */
	public boolean addUserDevice(Map<String, String> data) {
		boolean flag = false;
		try {
			// String openTime = data.get("open_time");
			// String closeTime = data.get("close_time");
			flag = new UserDeviceModel().set("wx_open_id", data.get("wx_open_id"))//
					.set("imei", data.get("imei"))//
					.set("sim_no", data.get("sim_no"))//
					.set("gear", data.get("gear"))//
					.set("device_status", data.get("device_status"))//
					.set("dampness", data.get("dampness"))//
					.set("temperature", data.get("temperature")).set("open_time", new Date()).save();
		} catch (Exception e) {
			logger.error("添加用户使用设备失败", e);
		}
		return flag;
	}

	/**
	 * 更新
	 * 
	 * @param data
	 * @return
	 */
	public boolean updateUserDevice(Map<String, String> data) {
		try {
			String wx_open_id = data.get("wx_open_id");
			String imei = data.get("imei");
			String closeTime = data.get("close_time");
			String device_status = data.get("device_status");
			List<UserDeviceModel> list = this.find(
					"select * from t_user_device where imei=?  and device_status = 0 ", imei
					);
			if (list != null && list.size() > 0) {
				logger.info("update t_user_device");
				return list.get(0)//
						.set("close_time", closeTime)//
						.set("device_status", device_status)//
						.set("wx_open_id", wx_open_id)
						.update();
			}
		} catch (Exception e) {
			logger.error("更新t_user_device状态失败", e);
		}
		return false;
	}

	public boolean updateUserDeviceGear(Map<String, Object> data) {
		logger.info("开机后获取机器传来的参数-----data：",data);
		try {
			String wx_open_id = (String) data.get("wx_open_id");
			String imei = (String) data.get("imei");
			String gear = (String) data.get("gear");
			String work_start_time1 = (String) data.get("work_start_time1"); // 开机时间1
			String work_close_time1 = (String) data.get("work_start_time1"); // 关机时间1
			String work_start_time2 = (String) data.get("work_start_time1"); // 开机时间2
			String work_close_time2 = (String) data.get("work_start_time1"); // 关机时间2
			String work_start_time3 = (String) data.get("work_start_time1"); // 开机时间3
			String work_close_time3 = (String) data.get("work_start_time1"); // 关机时间3
			boolean flag = this
					.find("select * from t_user_device where imei=? and device_status = 0 ", imei)
					.get(0)
					.set("gear", gear)//
					.set("work_start_time1", work_start_time1)//
					.set("work_close_time1", work_close_time1)//
					.set("work_start_time2", work_start_time2)//
					.set("work_close_time2", work_close_time2)//
					.set("work_start_time3", work_start_time3)//
					.set("work_close_time3", work_close_time3)//
					.set("update_date", new Date())//
					.update();
			return flag;
		} catch (Exception e) {
			logger.error("更新开关机状态失败", e);
		}
		return false;
	}

	// 获取开机信息
	public UserDeviceModel deviceOpenInfo(Map<String, Object> data) {
		String openid = (String) data.get("openid");
		String imei = (String) data.get("imei");
		try {
			logger.info("getDeviceOpenInfo t_user_device");
			List<UserDeviceModel> deviceOpenInfo= this.find(
					"select gear, work_start_time1, work_close_time1, work_start_time2,"
							+ "work_close_time2, work_start_time3, work_close_time3 "
							+ " from t_user_device where imei=?  and device_status = 0 ",
					imei);
			if (deviceOpenInfo != null && deviceOpenInfo.size() == 1) {
				return deviceOpenInfo.get(0);
			}

		} catch (Exception e) {
			logger.info("获取设备开机信息失败", e);
		}
		return null;
	}

	public List<UserDeviceModel> getDeviceWorkList(String imei) {
		return this.find("select * from t_user_device where imei = ?  and status = 'N' and device_status = 0",
				imei);
	}
	
	//更新定时时间
	public boolean updateWorkTime(Map<String, Object> timeResult) {
		try {
			logger.info("updateWorkTime t_user_device",timeResult);
			String openid = (String)timeResult.get("openid");
			String imei = (String)timeResult.get("imei");
			String startTime = (String) timeResult.get("startTime");
			String endTime = (String) timeResult.get("endTime");
			String timeType = (String) timeResult.get("timeType");
			boolean flag = false;
			if(timeType.equals("one")){
				flag = this
						.find("select * from t_user_device where imei=?  and device_status = 0 ", imei
								)
						.get(0).set("work_start_time1", startTime)//
						.set("work_close_time1", endTime)
						.set("wx_open_id", openid)
						.set("update_date", new Date())//
						.update();
				logger.info("updateWorkTime t_user_device ----time1",flag);
				return flag;
			}else if(timeType.equals("tow")){
				flag = this
						.find("select * from t_user_device where imei=?  and device_status = 0 ", imei
								)
						.get(0).set("work_start_time2", startTime)//
						.set("work_close_time2", endTime)
						.set("wx_open_id", openid)
						.set("update_date", new Date())//
						.update();
				logger.info("updateWorkTime t_user_device ----time2",flag);
				return flag;
			}else{
				flag = this
						.find("select * from t_user_device where imei=?  and device_status = 0 ", imei
								)
						.get(0).set("work_start_time3", startTime)//
						.set("work_close_time3", endTime)
						.set("wx_open_id", openid)
						.set("update_date", new Date())//
						.update();
				logger.info("updateWorkTime t_user_device ----time3",flag);
				return flag;
			}
			
		} catch (Exception e) {
			logger.error("更新定时时间失败", e);
		}
		return false;
	}
}
